package com.jus.player.adapter.my;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.jus.player.R;
import com.jus.player.activity.home.JsPlayerActivity;
import com.jus.player.config.AppConfig;
import com.jus.player.entity.VideoEntity;
import com.jus.player.entity.WatchHistory;
import com.jus.player.util.DialogUtils;
import com.jus.player.util.StringUtils;

import java.util.List;
import java.util.Locale;

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {

    private final List<WatchHistory> historyList;
    private Context mContext;
    private OnItemClickListener mListener;

    public HistoryAdapter(Context context, List<WatchHistory> data) {
        this.mContext = context;
        this.historyList = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_history_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        WatchHistory history = historyList.get(position);
        // 假设这里使用Glide加载视频缩略图
        if (!StringUtils.isEmpty(history.getVideoCover())) {
            Glide.with(holder.itemView.getContext())
                    .load(history.getVideoCover())
                    //.transform(new RoundedCorners(32))
                    .into(holder.thumbImg);
        }
        holder.title.setText(history.getVideoTitle());

        String episodeStr = mContext.getString(R.string.str_episode);
        String str1 = mContext.getString(R.string.str_no) + history.getEpisode() + episodeStr;
        String str2 = mContext.getString(R.string.str_total) + history.getTotal() + episodeStr;
        holder.episodeText.setText(str1 + " / " + str2);
        // 格式化进度显示
        int percent = formatProgress(history.getProgress(), history.getDuration());
        holder.progressText.setText(mContext.getString(R.string.str_already_watch) + " "
                + String.format(Locale.getDefault(), "%d%%", percent));
        holder.progressBar.setProgress(percent);
        // 格式化时间显示
        String timeText = DateUtils.getRelativeTimeSpanString(history.getLastWatchTime(),
                System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS).toString();
        holder.progressTime.setText(timeText);

        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onItemClick(history);
            }
        });

        holder.deleteButton.setOnClickListener(v -> {
            // 弹出确认对话框
            DialogUtils.showConfirmDialog(
                    mContext,
                    mContext.getString(R.string.delete_history),
                    mContext.getString(R.string.sure_delete_history),
                    () -> {
                        if (mListener != null) {
                            mListener.onDeleteClick(history);
                        }
                    });
        });
    }
    private int formatProgress(long progress, long duration) {
        if (duration <= 0) return 0;
        return  (int) (progress * 100 / duration);
    }
    @Override
    public int getItemCount() {
        return historyList.size();
    }

    public interface OnItemClickListener {
        void onItemClick(WatchHistory history);
        void onDeleteClick(WatchHistory history);
    }
    public void updateData(List<WatchHistory> newData) {
        historyList.clear();
        historyList.addAll(newData);
        notifyDataSetChanged();
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView thumbImg;
        public TextView title;
        public TextView episodeText;
        public TextView progressText;
        public TextView progressTime;
        public ProgressBar progressBar;
        public ImageButton deleteButton;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            thumbImg = itemView.findViewById(R.id.video_thumb);
            title = itemView.findViewById(R.id.title_text);
            episodeText = itemView.findViewById(R.id.episode_text);
            progressText = itemView.findViewById(R.id.progress_text);
            progressTime = itemView.findViewById(R.id.progress_time);
            deleteButton = itemView.findViewById(R.id.delete_button);
            progressBar = itemView.findViewById(R.id.progress_bar);
        }
    }
}
